package com.mlz.mlzmall.ui.activity.act.personal.userLogin;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Response;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.MyCenterUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.RequestUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyBaseActivity;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @description 重置登录密码
 * Created by 安帅 on 2016/5/24 19:59.
 */
public class ChangePasswordAgain extends VolleyBaseActivity implements View.OnClickListener {
    private static final String TGA = "ChangePasswordAgain";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lookingpassword);
        assignViews();
        setViewContent();
        setViewClickLinstener();
    }

    private TopTitleView forgetPwdTopTitle;
    private EditText actChangeNewpassword;
    private EditText etSurePassword;
    private Button btnPasswordOk;

    private void assignViews() {
        forgetPwdTopTitle = (TopTitleView) findViewById(R.id.forget_pwd_top_title);
        actChangeNewpassword = (EditText) findViewById(R.id.act_change_newpassword);
        etSurePassword = (EditText) findViewById(R.id.et_sure_password);
        btnPasswordOk = (Button) findViewById(R.id.btn_password_ok);
    }

    private void setViewContent() {
        forgetPwdTopTitle.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        forgetPwdTopTitle.setTitleButtonContent("重置登录密码", null, null);
    }

    private void setViewClickLinstener() {
        forgetPwdTopTitle.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {
            @Override
            public void onLeftBtnClick(View button) {
                finish();
            }
        });
        btnPasswordOk.setOnClickListener(this);
    }

    private String chageNewPwd, surePwd;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_password_ok:
                chageNewPwd = actChangeNewpassword.getText().toString().trim();
                surePwd = etSurePassword.getText().toString().trim();
                if (chageNewPwd.length() == 0 || surePwd.length() == 0) {
                    MyCenterUtils.ToastText("密码不能为空");
                    return;
                }
                executeRequest(RequestUtils.getMLZChangePwd(mContext, chageNewPwd, surePwd, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.i(TGA, "返回的数据" + jsonObject.toString());
                        try {
                            JSONObject json = new JSONObject(jsonObject.toString());
                            int status = json.getInt("status");
                            if (0 == status) {
                                Intent intent = new Intent(ChangePasswordAgain.this, SuccessedChangePassword.class);
                                enterActivityWithFinish(intent);
                            }
                            if (0 != status) {
                                String msg = json.getString("msg");
                                MyCenterUtils.ToastText(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }));
                break;
        }
    }
}
